package com.imoresoft.agile.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.IData;
import com.imoresoft.agile.entity.Result;
import com.imoresoft.magic.util.Constants;

public class SettingPersistAction extends BaseAction {

	private static final long	serialVersionUID	= -2528418471608896211L;

	private Result result = new Result();
	private transient Map<String, String>	style;

	public SettingPersistAction() {
	}
	
	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public Map<String, String> getStyle() {
		return style;
	}

	public void setStyle(Map<String, String> style) {
		this.style = style;
	}


	@SuppressWarnings("unchecked")
	public String persist() throws UnsupportedEncodingException {
		try {
			SuperDao superDao = new SuperDaoImpl(pd.getConn());
			IData d = new DataMap(style);
			d.put("USER_ID", pd.getTopUserId());
			d.put("NICK", pd.getTopNick());
			superDao.insert("TF_A_USER_STYLE", d);
		} catch (SQLException e) {
			result.setCode(-1);
			result.setMsg("样式保存出错，请重新保存！！！");
			logger.error("", e);
		} catch (Exception e) {
			result.setCode(-1);
			result.setMsg("样式保存出错，请重新保存！！！");
			logger.error("", e);
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private Map<String, String> init(Map<String, String> style) {
		IData data = new DataMap(style);
		data.put("intelligence_num_1", data.getString("intelligence_num_1",
				Constants.DEFAULT_INTELLIGENCE_NUMBER));
		data.put("intelligence_num_2", data.getString("intelligence_num_2",
				Constants.DEFAULT_INTELLIGENCE_NUMBER));
		data.put("intelligence_label_1", data.getString("intelligence_label_1",
				Constants.DEFAULT_INTELLIGENCE_LABEL_1));
		data.put("intelligence_label_2", data.getString("intelligence_label_2",
				Constants.DEFAULT_INTELLIGENCE_LABEL_2));
		data.put("intelligence_frequency", data.getString(
				"intelligence_frequency",
				Constants.DEFAULT_INTELLIGENCE_FREQUENCY));

		data.put("promote_radio", data.getString("promote_radio",
				Constants.DEFAULT_PROMOT_RADIO));
		data.put("promote_style", data.getString("promote_style",
				Constants.DEFAULT_PROMOT_STYLE));
		data.put("buy_button", data.getString("buy_button",
				Constants.DEFAULT_BUY_BUTTON));
		data.put("price_label", data.getString("price_label",
				Constants.DEFAULT_PRICE_LABEL));
		data.put("promote_title", data.getString("promote_title",
				Constants.DEFAULT_PROMOT_TITLE));
		data.put("to_be_promoted", data.getString("to_be_promoted",
				StringUtils.EMPTY));
		data.put("to_promote", data.getString("to_promote", StringUtils.EMPTY));
		data.put("num_per_line", data.getString("num_per_line",
				Constants.DEFAULT_NUM_PER_LINE));
		data.put("install_place", data.getString("install_place",
				Constants.DEFAULT_INSTALL_PLACE));
		return new HashMap(data);
	}
}
